package com.example.teaching_server.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 创建劳动活动请求DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "创建劳动活动请求参数")
public class CreateLaborActivityRequest {

    @NotBlank(message = "活动标题不能为空")
    @Size(min = 2, max = 100, message = "活动标题长度在2-100字符之间")
    @Schema(description = "活动标题", example = "春季校园植树活动", requiredMode = Schema.RequiredMode.REQUIRED)
    private String title;

    @NotBlank(message = "活动描述不能为空")
    @Size(max = 2000, message = "活动描述长度不能超过2000字符")
    @Schema(description = "活动详细描述", example = "春季校园环境美化植树活动")
    private String description;

    @Schema(description = "劳动基地ID", example = "2")
    private Long baseId;

    @Schema(description = "劳动基地名称", example = "校园劳动基地")
    private String baseName;

    @NotBlank(message = "活动地址不能为空")
    @Size(max = 500, message = "活动地址长度不能超过500字符")
    @Schema(description = "活动详细地址", example = "学校操场东侧", requiredMode = Schema.RequiredMode.REQUIRED)
    private String address;

    @NotNull(message = "开始日期不能为空")
    @FutureOrPresent(message = "开始日期不能是过去日期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "活动开始日期", example = "2024-03-15", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDate startDate;

    @NotNull(message = "结束日期不能为空")
    @FutureOrPresent(message = "结束日期不能是过去日期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "活动结束日期", example = "2024-03-15", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDate endDate;

    @PositiveOrZero(message = "总天数不能为负数")
    @Schema(description = "活动总天数", example = "1")
    private Integer totalDays;

    @JsonFormat(pattern = "HH:mm:ss")
    @Schema(description = "每日开始时间", example = "14:00:00")
    private LocalTime startTime;

    @NotNull(message = "报名截止时间不能为空")
    @Future(message = "报名截止时间必须是未来时间")
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    @Schema(description = "报名截止时间", example = "2024-03-10T18:00:00", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime registrationDeadline;

    @FutureOrPresent(message = "发布时间不能是过去时间")
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    @Schema(description = "活动发布时间", example = "2024-03-01T10:00:00")
    private LocalDateTime publishTime;

    @NotBlank(message = "目标受众不能为空")
    @Size(max = 200, message = "目标受众描述长度不能超过200字符")
    @Schema(description = "目标受众描述", example = "全体学生", requiredMode = Schema.RequiredMode.REQUIRED)
    private String targetAudience;

    @NotNull(message = "是否免费不能为空")
    @Schema(description = "是否免费活动", example = "true", requiredMode = Schema.RequiredMode.REQUIRED)
    private Boolean isFree;

    @NotNull(message = "最大参与人数不能为空")
    @Min(value = 1, message = "最大参与人数至少为1")
    @Max(value = 500, message = "劳动活动最大参与人数不能超过500")
    @Schema(description = "最大参与人数", example = "200", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer maxParticipants;

    @PositiveOrZero(message = "当前参与人数不能为负数")
    @Schema(description = "当前参与人数", example = "0")
    @Builder.Default
    private Integer currentParticipants = 0;

    @NotNull(message = "创建人ID不能为空")
    @Positive(message = "创建人ID必须是正整数")
    @Schema(description = "创建人用户ID", example = "123", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long creatorId;

    /**
     * 验证是否为单日活动
     */
    @Schema(description = "是否为单日活动", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isSingleDayActivity() {
        return startDate != null && endDate != null && startDate.equals(endDate);
    }

    /**
     * 验证劳动活动安全性（人数限制更严格）
     */
    public boolean isSafeForLaborActivity() {
        return maxParticipants != null && maxParticipants <= 500 &&
                (currentParticipants == null || currentParticipants <= maxParticipants);
    }

    /**
     * 获取活动时长（小时）
     */
    @Schema(description = "预计活动时长（小时）", accessMode = Schema.AccessMode.READ_ONLY)
    public Double getEstimatedDurationHours() {
        if (startTime != null && totalDays != null) {
            // 假设每天活动4小时
            return totalDays * 4.0;
        }
        return 4.0; // 默认4小时
    }
}
